<template>
  <el-dialog v-bind="{ ...$attrs }" class="app-edit" >
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px"  status-icon>
        <el-form-item label="网址/电话/Email" prop="web">
          <div class="edit-btn">
            <el-input v-model="ruleForm.target" />
            <el-button type="primary" @click="builderDialog=true">UTM Builder</el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-popover
            placement="bottom"
            title="参数功能"
            :width="600"
            trigger="click"
          >
            <template #reference>
              <el-button class="m-2"> <el-icon><QuestionFilled /></el-icon> 参数功能</el-button>
            </template>
            <div class="tips-content">
                    短网址创立后, 可在短网址后面输入任何自己想要的参数
                    <br>范例 -&nbsp;创好得到的短网址 =&gt;&nbsp;lihi.cc/kWgB7
                    <br>短网址后面可以随意加入自己想要的参数
                    <br>lihi.cc/kWgB7/<b style="color: red">JAY</b>
                    <br>lihi.cc/kWgB7/<b style="color: red">TONY</b>
                    <br>lihi.cc/kWgB7/<b style="color: red">参数自己随便填写</b>
                    <br>所有被点击过的连结, 只要有填写参数, 参数都会被记录在短网址数据内
                    <br>如果你想要把这个参数PASS到自己的网址内的话. 只要在自己的网址内加上<b style="color: red">{lihi}</b>就可以了
                    <br>假设你的网址是&nbsp;YourDomain.com, 你后面加了GA UTM后变成&nbsp;YourDomain.com/?utm_source=<b style="color: red">{lihi}</b>
                    <br>任何人点了&nbsp;lihi.cc/kWgB7/<b style="color: red">JAY</b>&nbsp;后, 参数就会被带进去, 最后到达网址就会变成&nbsp;YourDomain.com/?utm_source=<b style="color: red">JAY</b>
                    <br>你的GA就能记录到 <b style="color: red">JAY</b> 这个参数了
            </div>
          </el-popover>
        </el-form-item>
      </el-form>
      <template #footer>
          <el-button @click="$emit('close')"> 关闭 </el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)"> 确认 </el-button>
      </template>
      <Builder title="UTM Builder" v-model="builderDialog" @close="builderDialog=false" @success="getLineTarget"></Builder>
  </el-dialog>
</template>
 
<script lang="ts" setup>
import { reactive, ref ,withDefaults,defineEmits} from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import {QuestionFilled} from '@element-plus/icons-vue'
import Builder from './Builder.vue';
import { setLineDetail } from '@/server/home'
import { ElMessage } from 'element-plus'
//自定义属性
const props = defineProps({
  lineItem:{}
})
const emit = defineEmits<{(event: 'close'): void;(event: 'success'): void;  }>()
//定义属性
interface RuleForm{
  "id":string,
  "redirectTargetId":string,
  "target": string,
  "redirectType": number,
  "utmSource": string,
  "utmMedium": string,
  "utmCampaign": string,
  "utmTerm": string,
  "utmContent": string
}
//开关
const builderDialog = ref(false)
//表单
const formSize = ref('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>(props.lineItem)

const rules = reactive<FormRules<RuleForm>>({

})

//提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate( async (valid, fields) => {
    if (valid) {
      console.log('submit!',ruleForm)
      await setLineDetail(ruleForm.value)
      ElMessage.success("修改成功")
      emit("success")
    } else {
      ElMessage.error("修改失败")
      console.log('error submit!', fields)
    }
  })
}
//获取短网址
const getLineTarget= (e:string)=>{
  builderDialog.value=false
  ruleForm.value.target=e
}
console.log(props.lineItem)

</script>
<style lang="scss" scoped>
.edit-btn{
    display: flex;
    width: 100%;
}
.tips-content{
  padding: 10px;
  border-top: 1px solid #eee;
}
</style>